Imports Aricie.DNN.UI.WebControls.EditControls
Imports System.ComponentModel
Imports Aricie.DNN.UI.Attributes
Imports Aricie.Collections
Imports Aricie.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports System.Xml.Serialization
Imports Aricie.DNN.ComponentModel
Imports Aricie.DNN.Services
Imports DotNetNuke.Services.Localization

Namespace Aricie.DNN.Caching.Client

    <DefaultProperty("ClusterName")> _
    <Serializable()> _
    Public Class CachingClusterSettings
        Implements ITypedContainer
        Implements IEnabled



        Private _ClusterName As String = ""
        Private _Enabled As Boolean = True

        Private _ProviderName As String
        Private _PollingInterval As New STimeSpan(TimeSpan.FromSeconds(20))
        Private _ClientTimeout As New STimeSpan(TimeSpan.FromSeconds(5))
        Private _RecoverDuration As New STimeSpan(TimeSpan.FromSeconds(10))


        Private _Hosts As New SerializableList(Of CachingClientHostInfo)
        <NonSerialized()> _
        Private _ClientProvider As CachingClientProvider


        <Required(True)> _
        <MainCategory()> _
        Public Property ClusterName() As String
            Get
                Return _ClusterName
            End Get
            Set(ByVal value As String)
                _ClusterName = value
            End Set
        End Property

        <XmlIgnore()> _
           <MainCategory()> _
        Public ReadOnly Property CurrentStatus() As String
            Get
                Dim statusKey As String = Me.ClientProvider.CurrentStatus.ToString
                Return Localization.GetString(statusKey, NukeHelper.GetModuleSharedResourceFile(DistributedCachingBusinessController.ModuleDefId))
            End Get
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property LastErrors() As String
            Get
                Dim errors As List(Of KeyValuePair(Of String, String)) = Me.ClientProvider.LastErrors
                If errors.Count > 0 Then
                    Dim toReturn As New StringBuilder
                    For Each objPair As KeyValuePair(Of String, String) In errors
                        toReturn.Append(FormatError(objPair))
                    Next
                    Return toReturn.ToString
                End If
                Return "No DistributedCaching Error to report"
            End Get
        End Property


        <IsReadOnly(True)> _
        Public Property ProviderName() As String
            Get
                Return _ProviderName
            End Get
            Set(ByVal value As String)
                _ProviderName = value
            End Set
        End Property




        Public Property Enabled() As Boolean Implements IEnabled.Enabled
            Get
                Return _Enabled
            End Get
            Set(ByVal value As Boolean)
                _Enabled = value
            End Set
        End Property

        <ExtendedCategory("Connection")> _
             <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property PollingInterval() As STimeSpan
            Get
                Return _PollingInterval
            End Get
            Set(ByVal value As STimeSpan)
                _PollingInterval = value
            End Set
        End Property

        <ExtendedCategory("Connection")> _
              <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property ClientTimeout() As STimeSpan
            Get
                Return _ClientTimeout
            End Get
            Set(ByVal value As STimeSpan)
                _ClientTimeout = value
            End Set
        End Property

        <ExtendedCategory("Connection")> _
        <Editor(GetType(PropertyEditorEditControl), GetType(EditControl))> _
           <LabelMode(LabelMode.Top)> _
        Public Property RecoverDuration() As STimeSpan
            Get
                Return _RecoverDuration
            End Get
            Set(ByVal value As STimeSpan)
                _RecoverDuration = value
            End Set
        End Property


        '<SortOrder(10)> _
        <ExtendedCategory("Hosts")> _
            <Editor(GetType(ListEditControl), GetType(EditControl))> _
            <InnerEditor(GetType(PropertyEditorEditControl)), CollectionEditor(False, False, True, True, 10), LabelMode(LabelMode.Top)> _
        Public Property Hosts() As SerializableList(Of CachingClientHostInfo)
            Get
                Return _Hosts
            End Get
            Set(ByVal value As SerializableList(Of CachingClientHostInfo))
                _Hosts = value
            End Set
        End Property


        Public Function GetNewItem(ByVal collectionPropertyName As String) As Object Implements Aricie.DNN.UI.WebControls.EditControls.ITypedContainer.GetNewItem
            Select Case collectionPropertyName
                Case "Hosts"
                    Return Me.ClientProvider.GetNewHostItem
                Case Else
                    Return Nothing
            End Select
        End Function

        <XmlIgnore()> _
        <Browsable(False)> _
        Public Overridable ReadOnly Property ClientProvider() As CachingClientProvider
            Get
                If _ClientProvider Is Nothing Then
                    Dim providerConfig As CachingClientProviderInfo = Nothing
                    If DistributedCachingProviderBase.Settings.ClientSettings.AvailableProviders.TryGetValue(Me._ProviderName, providerConfig) Then
                        _ClientProvider = providerConfig.GetClientProvider
                        _ClientProvider.ProviderSettings = providerConfig
                        _ClientProvider.ClusterSettings = Me
                    Else
                        Throw New ApplicationException(String.Format("Provider {0} not available for cluster {1}", Me._ProviderName, Me._ClusterName))
                    End If
                End If
                Return _ClientProvider
            End Get
        End Property




        Private Function FormatError(ByVal errorPair As KeyValuePair(Of String, String)) As String
            Return String.Format("Error Status: ""{0}"", Message: ""{1}""", errorPair.Key, errorPair.Value) & vbCrLf
        End Function


    End Class
End Namespace